
package com.sausageking.client.common;

import java.io.Serializable;

import com.google.common.base.Strings;
import com.google.gwt.user.client.Cookies;
import com.sausageking.shared.Constants;

public class Context implements Serializable {

  private static final long serialVersionUID = -149230640624943904L;

  private String userEmail;

  private String sessionId;

  private String merchant;
  
  private boolean hasPassword;

  private int keepLastNCheckins;
  
  private int keepLastNRedemptions;

  
  public int getKeepLastNRedemptions() {
    return keepLastNRedemptions;
  }

  
  public Context setKeepLastNRedemptions(int keepLastNRedemptions) {
    this.keepLastNRedemptions = keepLastNRedemptions;
    return this;
  }

  public String getEmail() {
    return userEmail;
  }

  public boolean hasEmail() {
    return !Strings.isNullOrEmpty(userEmail);
  }

  public Context setEmail(String email) {
    this.userEmail = email;
    return this;
  }

  public String getSessionId() {
    return sessionId;
  }

  public Context setSessionId(String sessionId) {
    this.sessionId = sessionId;
    return this;
  }

  public boolean hasSessionId() {
    return !Strings.isNullOrEmpty(sessionId);
  }

  // TODO: validate session on server side
  public boolean isSessionValid() {
    //return hasEmail() && hasSessionId();
    return false;
  }

  public String getMerchant() {
    return merchant;
  }

  public Context setMerchantId(String merchantId) {
    this.merchant = merchantId;
    return this;
  }

  public boolean hasMerchant() {
    return !Strings.isNullOrEmpty(merchant);
  }

  public int getKeepLastNCheckins() {
    return keepLastNCheckins;
  }

  public Context setKeepLastNCheckins(int keepLastNCheckins) {
    this.keepLastNCheckins = keepLastNCheckins;
    return this;
  }

  public void persist() {
    if (hasEmail()) {
      Cookies.setCookie(Constants.EMAIL, getEmail());
    }
    if (hasSessionId()) {
      Cookies.setCookie(Constants.SESSION_ID, getSessionId());
    }
    if (hasPassword()) {
      Cookies.setCookie(Constants.HAS_PASSWORD, "true");
    }
  }

  public boolean hasPassword() {
    return hasPassword;
  }


  public Context setHasPassword(boolean hasPassword) {
    this.hasPassword = hasPassword;
    return this;
  }
}
